package com.xhwy.controller;

import com.xhwy.entity.ShoppingCart;
import com.xhwy.param.ShoppingCartSaveParam;
import com.xhwy.param.UserIdParam;
import com.xhwy.service.ShoppingCartService;
import com.xhwy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("add")
    public R add(@RequestBody @Validated ShoppingCartSaveParam shoppingCartSaveParam, BindingResult result){
        if(result.hasErrors()){
            return R.fail("请求参数异常，无法添加购物车!");
        }
        return shoppingCartService.add(shoppingCartSaveParam);
    }

    @GetMapping("list")
    public R list(@Validated UserIdParam userIdParam, BindingResult result){
        if (result.hasErrors()){
            return R.fail("购物车数据查询失败");
        }
        return shoppingCartService.getByUserId(userIdParam);
    }

    @PutMapping("update")
    public R update(@RequestBody ShoppingCart shoppingCart){

        return shoppingCartService.update(shoppingCart);
    }

    @DeleteMapping("/delete")
    public R delete(@RequestBody ShoppingCart shoppingCart){

        return shoppingCartService.delete(shoppingCart);
    }
}
